package com.github.pig.nideshop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.UserUtils;
import com.github.pig.nideshop.entity.NideshopAdmin;
import com.github.pig.nideshop.entity.NideshopSubscribe;
import com.github.pig.nideshop.entity.NideshopUser;
import com.github.pig.nideshop.mapper.NideshopAdminMapper;
import com.github.pig.nideshop.mapper.NideshopProductMapper;
import com.github.pig.nideshop.mapper.NideshopSubscribeMapper;
import com.github.pig.nideshop.mapper.NideshopUserMapper;
import com.github.pig.nideshop.service.INideshopGoodsService;
import com.github.pig.nideshop.service.INideshopProductService;
import com.github.pig.nideshop.service.INideshopSubscribeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.html.parser.Entity;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author J神
 * @since 2018-06-13
 */
@Service
public class NideshopSubscribeServiceImpl extends ServiceImpl<NideshopSubscribeMapper, NideshopSubscribe> implements INideshopSubscribeService {
    @Autowired
    private NideshopAdminMapper nideshopAdminMapper;
    @Autowired
    private NideshopSubscribeMapper nideshopSubscribeMapper;
    @Autowired
    private NideshopUserMapper nideshopUserMapper;
    @Autowired
    private INideshopGoodsService nideshopGoodsService;
    @Autowired
    private NideshopProductMapper nideshopProductMapper;
    @Override
    public List<String> allowTime(NideshopSubscribe nideshopSubscribe) {
        Integer adminId=nideshopSubscribe.getAdminId();
        EntityWrapper entityWrapper=new EntityWrapper();
        List<String> subscribeTime=new ArrayList();
        entityWrapper.eq("admin_id",adminId);
        entityWrapper.eq("subscribe_day",nideshopSubscribe.getSubscribeDay());
        List<NideshopSubscribe> subscribeList=nideshopSubscribeMapper.selectList(entityWrapper);
        for(NideshopSubscribe nideshopSubscribe1:subscribeList){
            subscribeTime.add(nideshopSubscribe1.getSubscribeTime());
        }
        NideshopAdmin nideshopAdmin=nideshopAdminMapper.selectById(adminId);
        return subscribeTime;
    }
    @Override
    public NideshopSubscribe create(NideshopSubscribe nideshopSubscribe) {
        List unallowTime=allowTime(nideshopSubscribe);
        if(unallowTime.contains(nideshopSubscribe.getSubscribeTime())){
            throw new RuntimeException("该时间不可预约");
        }
        NideshopUser nideshopUser=nideshopUserMapper.selectById(nideshopSubscribe.getUserId());
        nideshopSubscribe.setGmtCreate(new Date());
        nideshopSubscribe.setGmtUpdate(new Date());
        nideshopSubscribe.setUpdateBy("");
        nideshopSubscribe.setCreateBy("");
        nideshopSubscribe.setOpenId(nideshopUser.getWeixinOpenid());
        nideshopSubscribeMapper.insert(nideshopSubscribe);
        return nideshopSubscribe;
    }

}
